package dn.easy.common.core;

import android.app.Application;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import com.jess.arms.base.delegate.AppLifecycles;
import com.jess.arms.di.module.GlobalConfigModule;
import com.jess.arms.http.log.RequestInterceptor;
import com.jess.arms.integration.ConfigModule;
import java.util.List;
import dn.easy.common.commonutils.SPUtils;
import dn.easy.common.commonutils.StringUtils;
import dn.easy.common.constant.Contans;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @Multipart
 * @POST("fileService") Call<User> uploadFile(@Part MultipartBody.Part file);
 * File file = new File(filePath);
 * RequestBody body = RequestBody.create(MediaType.parse("application/otcet-stream"), file);
 * MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), body);
 * @Streaming
 * @GET Observable<ResponseBody> download(@Url String url);
 */
public class GlobalConfiguration implements ConfigModule {

//    String sessionid = "";

    @Override
    public void applyOptions(Context context, GlobalConfigModule.Builder builder) {
        builder.printHttpLogLevel(RequestInterceptor.Level.ALL);
        builder.baseurl(Contans.BaseUrl)
            .globalHttpHandler(new GlobalHttpHandlerImpl(context))
            .responseErrorListener(new ResponseErrorListenerImpl())
            .gsonConfiguration((context1, gsonBuilder) -> {//这里可以自己自定义配置Gson的参数
                gsonBuilder
                    .serializeSpecialFloatingPointValues()
                    .serializeNulls()//支持序列化null的参数
                    .enableComplexMapKeySerialization();//支持将序列化key为object的map,默认只能序列化key为string的map
            })
            // 自定义头部信息
            .addInterceptor(chain -> {
                /*if(!StringUtils.isEmpty(SPUtils.getSharedStringData(context,Contans.SSESSIONID))){
                    sessionid = SPUtils.getSharedStringData(context,Contans.SSESSIONID);
                }*/
                Request request = chain.request()
                    .newBuilder()
                    .addHeader("Content-Type", "application/json; charset=UTF-8")
                    .addHeader("Connection", "keep-alive")
//                  .addHeader("sessionid", sessionid)
                    .build();
                return chain.proceed(request);
            })
            .addInterceptor(chain -> {
                    Request request = chain.request();
                    Response response = chain.proceed(request);
                    if (response.code() == 200){
                        //存入sessionid
                        /*if (response.header("sessionid") != null) {
                            sessionid = response.header("sessionid");
                            SPUtils.setSharedStringData(context, Contans.SSESSIONID, sessionid);
                        }*/
                    }
                return response;
                })
            .rxCacheConfiguration((context1, rxCacheBuilder) -> {//这里可以自己自定义配置RxCache的参数
                rxCacheBuilder.useExpiredDataIfLoaderNotAvailable(true);
                return null;
            });
    }

    @Override
    public void injectAppLifecycle(Context context, List<AppLifecycles> lifecycles) {
        //向Application的生命周期中注入一些自定义逻辑
        lifecycles.add(new AppLifecyclesImpl());
    }

    @Override
    public void injectActivityLifecycle(Context context, List<Application.ActivityLifecycleCallbacks> lifecycles) {
        //向Activity的生命周期中注入一些自定义逻辑
        lifecycles.add(new ActivityLifecycleCallbacksImpl());
    }

    @Override
    public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
        //向Fragment的生命周期中注入一些自定义逻辑
    }
}
